package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class QingHuaWenQuan {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "acw_tc=276aede615813883746888248e7035659c81e374f130ae1bf8dc8ff57b7910; _gid=400406018657; _gidv=e883b55343a514a5faa0b47693217c93; PHPSESSID=fc3b2o446u0b7s4i51oeci0r0h; Hm_lvt_a84b27ffd87daa3273555205ef60df89=1582192183,1582195769,1582249732,1582249752; Hm_lpvt_a84b27ffd87daa3273555205ef60df89=1582250415; ssxmod_itna2=7qUxc7eYw4gDzxAxeuk8DB0QD7ArQ4UvxtG9Qv8+DBTx7IHGaCKmakvmx8rsSpaezuInfxNhH71CyQKjI6ThQqjI0hv6oejReu6xjjoZKyA/4w6HOLR09hp8n6HL+UU4UCdMPKZ7aYjEzdAhUxAHWTxqmxqt7h21efx2Bh2LCrvz/8dLKM50jbTtg+Dm7T5jKYQh0fuongbU9f2iXG3W=aFYyLuxISpKn84PIYtzno+fmlntMgTtqL5gn+LzIb3+jI9IIkTmC1M2+ikFSrAIIROTpSbE2KWVSEhXIIAxjw1PeRXAWBdVGtP73Veo7Yqu0q5Cw5o0OionE4fAvz733o5Kg5gD0woPTrDTDG28iqieF+Dxjh4BDwYo+bF3ExdrAxD08DiQtYD=; ssxmod_itna=iqmhY5AIPUxGx7q0LxYKO=x02mC9qImxQQD/RmDnqD=GFDK40ooyx=5o454N=KTzDQqKYtC7S0rWKrbD+KTYu1x0aDbqGk4oGEdGGmxBYDQxAYDGDDPODj4ibDYSZHn+xWKDwDlKDgDQ919xDaA2GitGFCHcy1VxD14PHPaGKD9x0CDla48IYD06OvtDi3IY9zMx0OD0IGXpcsDB+tq74xmmqx8YhiyF2qYAxtl4xqijh2qCiX5jqe4Er4Ka4a7YD===";
    public static final String ORIGIN = "https://lib-nuanxin.wqxuetang.com";
    public static final String REFERER = "https://lib-nuanxin.wqxuetang.com/search?cnclass=A";


    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum = 1;
        int totalPage = 3125;
        int pageSize = 10;
        for (int i = pageNum; i <= totalPage; i++) {
            String url = "https://lib-nuanxin.wqxuetang.com/v1/search/initsearch";
            Map<String, Object> parameter = MapUtil.newHashMap(7);
            parameter.put("type", "0");
            parameter.put("pn", i);
            parameter.put("isfquery", "undefined");
            parameter.put("size", pageSize);
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Origin", ORIGIN);
            headers.put("Referer", REFERER);
            String result = HttpUtil.doPost(url, headers, parameter);
            if (StrUtil.isNotBlank(result)) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                Integer errcode = jsonObject.getInteger("errcode");
                if (errcode != null && errcode == 0) {
                    JSONObject dataJsonObject = jsonObject.getJSONObject("data");
                    JSONArray listObject = dataJsonObject.getJSONArray("list");
                    int size = listObject.size();
                    List<CxgBook> listBook = new ArrayList<>();
                    for (int k = 0; k < size; k++) {
                        JSONObject jsonTmp = listObject.getJSONObject(k);
                        CxgBook bookEntity = getBookEntity(jsonTmp.getInteger("numid"));
                        bookEntity.setAwards(jsonTmp.getString("subname"));
                        bookEntity.setCatalog(jsonTmp.getString("catalog"));
                        bookEntity.setPressName("清华大学出版社");
                        bookEntity.setPressId(11);
                        bookEntity.setIsSync(0);
                        bookEntity.setStatus(1);
                        log.info(bookEntity.getBookName());
                        listBook.add(bookEntity);
                    }
                    bookService.saveBatch(listBook);
                    log.info("第" + i + "页数据保存成功");
                    log.warn("清华社--->" + i);
                }
            }
        }
    }

    public CxgBook getBookEntity(Integer numId) throws Exception {
        CxgBook bookEntity = null;
        String url = "https://lib-nuanxin.wqxuetang.com/v1/book/initbook";
        Map<String, Object> query = MapUtil.newHashMap(2);
        query.put("bid", numId);
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("Cookie", COOKIE);
        headers.put("User-Agent", UserAgentUtil.random());
        headers.put("Origin", ORIGIN);
        headers.put("Referer", REFERER);
        String result = HttpUtil.doGet(url, headers, query);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Integer errcode = jsonObject.getInteger("errcode");
        if (errcode != null && errcode == 0) {
            JSONObject entityJsonObject = jsonObject.getJSONObject("data");
            bookEntity = new CxgBook();
            bookEntity.setPkId(entityJsonObject.getString("bid"));
            bookEntity.setBookIsbn(entityJsonObject.getString("isbn"));
            bookEntity.setAuthor(entityJsonObject.getString("author"));
            String bookName = entityJsonObject.getString("name");
            bookEntity.setBookName(bookName);
            bookEntity.setCover(entityJsonObject.getString("coverurl"));
            bookEntity.setBrief(entityJsonObject.getString("descript"));
            bookEntity.setCatalog(entityJsonObject.getString("catalog"));
            String truePages = entityJsonObject.getString("true_pages");
            int pageNum = 0;
            if (StrUtil.isNotBlank(truePages)) {
                String numbers = getNumbers(truePages);
                if (StrUtil.isNotBlank(numbers)) {
                    pageNum = Integer.parseInt(numbers);

                }
            }
            bookEntity.setPageNum(pageNum);
            String words = entityJsonObject.getString("words");
            int wordNum = 0;
            if (StrUtil.isNotBlank(words)) {
                wordNum = Integer.parseInt(getNumbers(words));
            }
            bookEntity.setWordNum(wordNum);
            String pubDate = entityJsonObject.getString("date");
            if (StrUtil.isNotBlank(pubDate)) {
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm:ss");
                LocalDateTime localDateTime = LocalDateTime.parse(pubDate, dtf);
                bookEntity.setPubDate(localDateTime);
            }
            bookEntity.setPrice(entityJsonObject.getBigDecimal("price"));
            bookEntity.setDigitalUri("https://lib-nuanxin.wqxuetang.com/read/pdf/" + numId);

        }
        return bookEntity;
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
